package me.wangcai.trafficpoliceai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.wangcai.trafficpoliceai.model.ResponseInfo;
import me.wangcai.trafficpoliceai.model.entity.Appeal;
import me.wangcai.trafficpoliceai.model.entity.User;
import me.wangcai.trafficpoliceai.model.entity.Violation;
import me.wangcai.trafficpoliceai.model.request.AppealRequest;
import me.wangcai.trafficpoliceai.model.request.HandleRequest;
import me.wangcai.trafficpoliceai.model.request.PageRequest;
import me.wangcai.trafficpoliceai.service.IAppealService;
import me.wangcai.trafficpoliceai.service.IUserService;
import me.wangcai.trafficpoliceai.service.IViolationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/violation")
public class ViolationController {
    @Autowired
    private IViolationService violationService;
    @Autowired
    private IAppealService appealService;
    @Autowired
    private IUserService userService;

    @PostMapping("/findNotProcessed")
    public IPage<Map<String,Object>> findNotProcessed(@RequestBody PageRequest pageRequest){
        QueryWrapper<Appeal> wrapper = Wrappers.query();
        wrapper.isNull("handle_time");
        IPage<Map<String,Object>> page = appealService.pageMaps(new Page<>(pageRequest.getPage(), pageRequest.getAmount()), wrapper);
        for (Map<String, Object> record : page.getRecords()) {
            record.put("violation",violationService.getById(record.get("violation_id").toString())) ;
        }
        return page;
    }

    @PostMapping("/findUserAppeal")
    public ResponseInfo findUserAppeal(Principal principal){
        QueryWrapper<Appeal> wrapper = new QueryWrapper<>();
        wrapper.eq("username",principal.getName());
        return ResponseInfo.success("查询成功!",appealService.list(wrapper));
    }

    @PostMapping("/findUserViolation")
    public ResponseInfo findUserViolation(Principal principal){
        String number = userService.getUserByName(principal.getName()).getCarId();
        if(number == null) return ResponseInfo.error("车牌未绑定");
        QueryWrapper<Violation> wrapper = new QueryWrapper<>();
        wrapper.eq("carId",number);
        wrapper.eq("is_handle",0);
        return ResponseInfo.success("查询成功!",violationService.list(wrapper));
    }

    @PostMapping("/findAll")
    public IPage<Map<String,Object>> findAll(@RequestBody PageRequest pageRequest){
        IPage<Map<String,Object>> page = violationService.pageMaps(new Page<>(pageRequest.getPage(), pageRequest.getAmount()), new QueryWrapper<Violation>().eq("is_handle",0));
        return page;
    }

    @PostMapping("/add")
    public ResponseInfo addViolation(@RequestBody Violation violation){
        return violationService.add(violation);
    }

    @PostMapping("/handle")
    public ResponseInfo handle(Principal principal,@RequestBody HandleRequest handleRequest){
        return appealService.handle(handleRequest.getId(),principal.getName(),handleRequest.isAccept());
    }

    @PostMapping("/appeal")
    public ResponseInfo appeal(Principal principal,@RequestBody AppealRequest appealRequest){
        QueryWrapper<Appeal> wrapper = Wrappers.query();
        wrapper.eq("violation_id",appealRequest.getId());
        if(appealService.getOne(wrapper) != null) return ResponseInfo.error("已经申诉了 请不要重复申诉！！!");
        Violation violation = violationService.getById(appealRequest.getId());
        List<User> user = userService.list(new QueryWrapper<User>().eq("carId", violation.getCarId()));
        Appeal appeal = new Appeal();
        appeal.setViolationId(appealRequest.getId());
        appeal.setTime(LocalDate.now());
        appeal.setPass(false);
        appeal.setUsername(user.size() > 0 ? user.get(0).getUsername() : principal.getName());
        appeal.setReason(appealRequest.getReason());
        if(appealService.save(appeal)){
            return ResponseInfo.success("申诉成功,等待审核");
        }else{
            return ResponseInfo.error("提交失败");
        }

    }

    @PostMapping("/filter")
    public ResponseInfo filterViolation(@RequestBody HashMap<String,String> map){
        return violationService.filterViolation(map);
    }
    @PostMapping("/filterAppeal")
    public ResponseInfo filterAppeal(@RequestBody HashMap<String,String> map){
        return appealService.filterAppeal(map);
    }

//
//    @PostMapping("/deleteViolation/{id}")
//    private ResponseInfo delete(@PathVariable Integer id, Principal principal){
//        String username=principal.getName();
//        return violationService.updateViolation(id,username);
//    }
}
